package com.gold.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gold.entity.Card;
import com.gold.mapper.CardMapper;
import com.gold.myexception.Myexception;
import com.gold.service.CardService;
import com.gold.util.Myutil;

@Service
public class CardServiceImpl implements CardService {

	@Autowired
	private CardMapper cm;

	public List<Card> selectAllCard() {
		return cm.selectAllCard();
	}

	public List<Card> selectAllCardByPage(Map<String, String> map) {
		int pageSize = 5;
		int userCount = cm.selectAllCard().size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return cm.selectAllCardByPage(map);
	}

	public List<Card> selectCardByLike(Map<String, String> map) {

		return cm.selectCardByLike(map);
	}

	public void updateCardStatusEndJob() {
		cm.updateCardStatusEndJob();
	}

	public void updateCardStatusStartJob() {
		cm.updateCardStatusStartJob();
	}

	public void insertCard(Map<String, String> map, String[] cardProvIdString) throws Myexception {
		int cardCount = Integer.parseInt(map.get("cardCountString"));
		int cardAmount = Integer.parseInt(map.get("cardAmountString"));
		int cardStatusId = 2;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date cardStartTime = null;
		Date cardEndTime = null;
		try {
			cardStartTime = sdf.parse(map.get("cardStartTime"));
			cardEndTime = sdf.parse(map.get("cardEndTime"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Long startTime = cardStartTime.getTime();
		Long endTime = cardEndTime.getTime();
		if (startTime > endTime) {
			throw new Myexception("时间错误");
		}
		Long nowTime = System.currentTimeMillis();
		if (nowTime < startTime) {
			cardStatusId = 1;
		}
		if (nowTime > endTime) {
			cardStatusId = 4;
		}
		for (int j = 0; j < cardProvIdString.length; j++) {
			int cardProvId = Integer.parseInt(cardProvIdString[j]);
			for (int i = 0; i < cardCount; i++) {
				String cardNumber = i + Myutil.getDateString();
				String cardPwd = UUID.randomUUID().toString().split("[-]")[0];
				Card card = new Card(0, cardNumber, cardPwd, cardAmount, cardProvId, null, map.get("cardStartTime"), map.get("cardEndTime"), cardStatusId, null, null);
				cm.insertCard(card);
			}

		}

	}

	public void updateCardStatusBetweenJob() {
		cm.updateCardStatusBetweenJob();
	}

	public Card selectCardByCardNumber(String cardNumber) {
		return cm.selectCardByCardNumber(cardNumber);
	}

	public Card selectCardByCardId(int cardId) {
		return cm.selectCardByCardId(cardId);
	}

}
